#include "CF_Core.h"

CCF_Interface::CCF_Interface(void)
{
}

CCF_Interface::~CCF_Interface(void)
{
}

bool CCF_Interface::Initialize( LPDIRECT3DDEVICE9 device )
{
  m_device = device;

  if( m_device != NULL )
  {
    D3DXCreateSprite( m_device, &m_sprite );
  }

  return (m_device != NULL)&&(m_sprite != NULL);
}

void CCF_Interface::Add( LPCF_CONTROL control )
{
  this->m_controls.push_back( control );
}

void CCF_Interface::Remove( LPCF_CONTROL control )
{
  this->m_controls.remove( control );
}

void CCF_Interface::Render()
{
  m_sprite->Begin(D3DXSPRITE_ALPHABLEND);
  CCF_ControlList::iterator iterator = m_controls.begin();

  while( iterator != m_controls.end())
  {
    RECT rect = (*iterator)->GetBounds();
    LPDIRECT3DTEXTURE9 texture = (*iterator)->Render();

    if( texture != NULL )
    {
      m_sprite->Draw( texture, &rect, NULL, NULL, 0xFFFFFFFF );
    }

    iterator++;
  }

  m_sprite->End();
}

CCF_Control* CCF_Interface::HitTest( int x, int y )
{
  CCF_ControlList::iterator iterator = m_controls.begin();

  while( iterator != m_controls.end())
  {
    RECT rect = (*iterator)->GetBounds();

    if((rect.left < x)&&(x < rect.right)
      &&(rect.top < y)&&(y < rect.bottom))
    {
      return (*iterator);
    }

    iterator++;
  }

  return NULL;
}
